package com.yssoft.service.core;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.yssoft.entity.core.TDepartment;
import com.yssoft.entity.core.TProductImg;
import com.yssoft.entity.core.TProducts;
import com.yssoft.entity.core.TSemiFinishedGoods;
import com.yssoft.entity.core.TSemiImg;
import com.yssoft.mapper.core.TProductClassifyMapper;
import com.yssoft.mapper.core.TProductsMapper;
import com.yssoft.mapper.core.TSemiFinishedGoodsMapper;
import com.yssoft.mapper.core.TSemiImgMapper;
import com.yssoft.shiro.MyRealm.ShiroUser;
import com.yssoft.utils.LayuiUtil;
import com.yssoft.utils.UserHelper;
import com.yssoft.vo.TProductClassifyVo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 陈翼东
 * @说明 半成品
 * @time 2018/4/27 上午 10:27
 */
@Service
public class SemiFinishedGoodsService {

    @Resource
    private TSemiFinishedGoodsMapper semiFinishedGoodsMapper;
    @Autowired
	protected TProductsMapper productsMapper;
	@Autowired
	protected TProductClassifyMapper productClassifyMapper;
	@Resource
	private TSemiImgMapper tSemiImgMapper;
	
    /**
     * 分页查询半成品信息
     * @param page
     * @param limit
     * @param name
     * @param manufacturer 生产厂家
     * @return
     */
    public JSONObject querySemiFinishedGoodsPage(int page, int limit, String name, String manufacturer) {
        Integer companyId = UserHelper.getCurrentCompanyId();//当前登陆人企业
        PageHelper.startPage(page,limit);
        Map<String,Object> params=new HashMap<>();
        params.put("companyId" , companyId);
        params.put("name",name);
        params.put("manufacturer",manufacturer);
        List<TSemiFinishedGoods> pagelist = semiFinishedGoodsMapper.querySemiFinishedGoodsPage(params);
        int pageCount = semiFinishedGoodsMapper.querySemiFinishedGoodsCount(params);
        try {
            return LayuiUtil.getJSONObjectByObject(pagelist,pageCount,"","");
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    /**
	 * @方法名称: addAcc
	 * @功能描述: 新增产品
	 * @作者: 洪丰
	 * @创建时间: 2018年4月27日 上午9:47:47
	 * @param entity void
     * @param tMaterialIds2 
     * @param tImgId 
	 */
	public void addAcc(TSemiFinishedGoods entity, String imgId, String tImgId, String tMaterialIds) {
		
		// 取得当前登录人
		ShiroUser user = UserHelper.getCurrentUser();
		entity.setCreatorId(user.getId());
		entity.setCreatorName(user.getName());
		/*entity.setDeptId(user.getDept_id());
		entity.setDeptName(user.getDept_name());*/
		entity.setCreateTime(new Date());
		entity.setDeleteFlag("0");
		entity.setT_e_id(user.getEntId());
		semiFinishedGoodsMapper.insertSelectiveById(entity);
		if(!"".equals(imgId)){
			String[] imgIdArr = imgId.split(",");
			String[] tImgIdArr = tImgId.split(",");
			for(int i = 0; i < imgIdArr.length; i++){
				TSemiImg imgEntity = new TSemiImg();
				imgEntity.setCreateTime(new Date());
				imgEntity.setDeleteFlag("0");
				
				imgEntity.setPictureImg(imgIdArr[i]);
				imgEntity.setPictureImgT(tImgIdArr[i]);
				imgEntity.settSId(entity.getId());
				tSemiImgMapper.insert(imgEntity);
			}
		}
		/*保存半成品和物料中间表*/
		if(tMaterialIds != null && !"".equals(tMaterialIds)){
			String[] tMaterialId = tMaterialIds.split(",");
			for(String s : tMaterialId){
				semiFinishedGoodsMapper.insertTMaterial(entity.getId(), Integer.parseInt(s));
			}
		}
	}
	
	/**
	 * @方法名称: editAcc
	 * @功能描述: 修改产品
	 * @作者: 洪丰
	 * @创建时间: 2018年4月27日 上午9:49:44
	 * @param entity void
	 */
	public void editAcc(TSemiFinishedGoods entity, String imgId, String tImgId, String tMaterialIds) {
		/*修改半成品数据*/
		semiFinishedGoodsMapper.updateByPrimaryKeySelective(entity);
		/*删除半成品与物料中间数据*/
		semiFinishedGoodsMapper.deleteTMaterialById(entity.getId());
		if(!"".equals(imgId)){
			String[] imgIdArr = imgId.split(",");
			String[] tImgIdArr = tImgId.split(",");
			for(int i = 0; i < imgIdArr.length; i++){
				TSemiImg imgEntity = new TSemiImg();
				imgEntity.setCreateTime(new Date());
				imgEntity.setDeleteFlag("0");
				
				imgEntity.setPictureImg(imgIdArr[i]);
				imgEntity.setPictureImgT(tImgIdArr[i]);
				imgEntity.settSId(entity.getId());
				tSemiImgMapper.insert(imgEntity);
			}
		}
		/*保存半成品和物料中间表*/
		if(tMaterialIds != null && !"".equals(tMaterialIds)){
			String[] tMaterialId = tMaterialIds.split(",");
			for(String s : tMaterialId){
				semiFinishedGoodsMapper.insertTMaterial(entity.getId(), Integer.parseInt(s));
			}
		}
	}
	
	/**
	 * @方法名称: delAccById
	 * @功能描述: 逻辑删除产品
	 * @作者: 洪丰
	 * @创建时间: 2018年4月27日 上午9:50:06
	 * @param id
	 * @return int
	 */
	public int delAccById(Integer id) {
		TSemiFinishedGoods entity = new TSemiFinishedGoods();
		entity.setId(id);
		entity.setDeleteFlag("1");
		return semiFinishedGoodsMapper.updateByPrimaryKeySelective(entity);
	}
	
	/**
	 * @方法名称: queryProductsByRecord
	 * @功能描述: 通过record查询产品
	 * @作者: 洪丰
	 * @创建时间: 2018年4月28日 上午11:32:27
	 * @param record
	 * @return int
	 */
	public int queryProductsByRecord(TProducts record){
		record.setDeleteFlag("0");
		List<TProducts> roleLsit = productsMapper.select(record);
		return roleLsit.size();
	}
	
	/**
	 * @方法名称: queryProductById
	 * @功能描述: 获取唯一产品数据
	 * @作者: 洪丰
	 * @创建时间: 2018年4月27日 上午9:34:46
	 * @param id
	 * @return TProducts
	 */
	public TSemiFinishedGoods queryProductById(Integer id){
		return semiFinishedGoodsMapper.selectByPrimaryKey(id);
	}

	/**
	 * 根据半成品与物料id删除中间数据
	 * @param sId
	 * @param mIds
	 */
	public void delSemMater(String sId, String mIds) {
		if(mIds!=null && !"".equals(mIds)){
			List<Map<String,Object>> list = new ArrayList<>();
			String[] mId = mIds.split(",");
			for(String s : mId){
				Map<String,Object> map = new HashMap<>();
				map.put("sId", sId);
				map.put("mId", s);
				list.add(map);
			}
			semiFinishedGoodsMapper.delSemMater(list);
		}
	}
	
	/**
	 * 根据半成品与物料id添加中间数据
	 * @param sId
	 * @param mIds
	 */
	public void insSemMater(String sId, String mIds) {
		if(mIds!=null && !"".equals(mIds)){
			String[] mId = mIds.split(",");
			List<Map<String,Object>> list = new ArrayList<>();
			for(String s : mId){
				Map<String,Object> map = new HashMap<>();
				map.put("sId", sId);
				map.put("mId", s);
				list.add(map);
			}
			semiFinishedGoodsMapper.insSemMater(list);
		}
	}

	/**
	 * @方法名称: findProductImgList
	 * @功能描述: 通过产品ID获取产品图片
	 * @作者: 洪丰
	 * @创建时间: 2018年4月27日 下午6:25:13
	 * @param ProductId
	 * @return List<TProductImg>
	 */
	public List<TSemiImg> findProductImgList(int ProductId) {
		TSemiImg record = new TSemiImg();
		record.settSId(ProductId);
		return tSemiImgMapper.select(record);
	}
	
	/**
	 * @方法名称: delImgById
	 * @功能描述: 删除图片
	 * @作者: 洪丰
	 * @创建时间: 2018年4月27日 下午6:42:07
	 * @param id
	 * @return int
	 */
	public int delImgById(Integer id) {
		return tSemiImgMapper.deleteByPrimaryKey(id);
	}
}
